package sg.edu.nus.iss.vmcs.system;

import sg.edu.nus.iss.vmcs.store.CashStoreItem;
import sg.edu.nus.iss.vmcs.store.Coin;
import sg.edu.nus.iss.vmcs.store.IStoreLoader;
import sg.edu.nus.iss.vmcs.store.ItemLoader;
import sg.edu.nus.iss.vmcs.store.StoreItem;

public class CashItemLoader extends ItemLoader {
	
	private static final String NAME_LABEL     = "Name";
	private static final String WEIGHT_LABEL   = "Weight";
	private static final String VALUE_LABEL    = "Value";
	private static final String QUANTITY_LABEL = "Quantity";

	public CashItemLoader(IStoreLoader loader) {
		super(loader);
	}

	@Override
	public StoreItem getItem(int index) {
		int idx = index + 1;
		Coin coin = new Coin();

		String name = new String(NAME_LABEL + idx);
		String value = getStoreLoader().getValue(name);
		coin.setName(value);

		name = new String(WEIGHT_LABEL + idx);
		value = getStoreLoader().getValue(name);
		coin.setWeight(Double.parseDouble(value));

		name = new String(VALUE_LABEL + idx);
		value = getStoreLoader().getValue(name);
		coin.setValue(Integer.parseInt(value));

		name = new String(QUANTITY_LABEL + idx);
		value = getStoreLoader().getValue(name);
		int qty = Integer.parseInt(value);

		CashStoreItem item = new CashStoreItem(coin, qty);
		return item;
	}

	@Override
	public void setItem(int index, StoreItem cashItem) {
		int idx = index + 1;

		CashStoreItem item = (CashStoreItem) cashItem;
		Coin cn = (Coin) item.getContent();
		String itn = new String(NAME_LABEL + idx);

		getStoreLoader().setValue(itn, cn.getName());

		itn = new String(WEIGHT_LABEL + idx);
		getStoreLoader().setValue(itn, String.valueOf(cn.getWeight()));

		itn = new String(VALUE_LABEL + idx);
		getStoreLoader().setValue(itn, String.valueOf(cn.getValue()));

		itn = new String(QUANTITY_LABEL + idx);
		getStoreLoader().setValue(itn, String.valueOf(item.getQuantity()));

	}

}
